package com.dcm.item.controller;

import com.dcm.common.annotation.EnableResponseBodyAdvice;
import com.dcm.common.annotation.Log;
import com.dcm.item.client.UploadClient;
import com.dcm.item.entity.ItemEntity;
import com.dcm.item.service.ItemService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author pengrongsheng
 */
@Api(tags = "ItemController")
@RestController
@RequestMapping("/item")
@EnableResponseBodyAdvice
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Autowired
    private UploadClient uploadClient;

    @ApiOperation("获取所有商品")
    @GetMapping("/findAll")
    @Log(title = "findAll",action = "获取所有商品")
    public List<ItemEntity> findAll() {
        return itemService.findAll();
    }

    /**
     * 添加商品
     */
    @ApiOperation("添加商品")
    @PostMapping("/addItem")
    public void addItem(@RequestBody ItemEntity item) {
        itemService.addItem(item);
    }


    @ApiOperation("根据关键字或id查询")
    @GetMapping("/findItemByKeyWords")
    @Log(title = "findItemByKeyWords",action = "根据关键字或id查询")
    public List<ItemEntity> findItemByKeyWords(@RequestParam(value = "keyWords", required = false) String keyWords) {
        return itemService.findItemByKeyWords(keyWords);
    }

    /**
     * 根据 状态 0 在售 1 仓库 2售空 -1回收站
     */
    @ApiOperation("状态：0 在售 ，1 仓库 ，2售空 ，-1回收站")
    @GetMapping("/findItemByStatus")
    public List<ItemEntity> findItemByStatus(@RequestParam(value = "status", required = false) Integer status) {
        return itemService.findItemByStatus(status);
    }

    @ApiOperation("商品上架下架，加入回收站")
    @PutMapping("/updateItemStatus")
    public void updateItemStatus(@RequestBody ItemEntity item) {
        itemService.updateItemStatus(item);
    }

    /**
     * 根据 specId id itemName 查询
     *
     * @param specId
     * @param id
     * @param itemName
     * @return
     */
    @ApiOperation("多条件查询")
    @GetMapping("/searchItem")
    public List<ItemEntity> searchItem(@RequestParam(value = "specId", required = false) Integer specId,
                                       @RequestParam(value = "id", required = false) Long id,
                                       @RequestParam(value = "itemName", required = false) String itemName) {
        return itemService.searchItem(specId, id, itemName);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @ApiOperation("根据 id查询 商品")
    @GetMapping("/findItemById")
    public ItemEntity findItemById(@RequestParam(value = "id", required = false) Long id) {
        return itemService.findItemById(id);
    }

    @ApiOperation("分页")
    @GetMapping("/findPage")
    public PageInfo<ItemEntity> findPage(@RequestParam(value = "pageSize", required = false) Integer pageSize,
                                         @RequestParam(value = "currentPage", required = false) Integer currentPage) {
        return itemService.findPage(currentPage, pageSize);
    }

}
